fluent-uri
A generic URI/IRI handling library compliant with RFC 3986 and RFC 3987. It is:
- Fast: Zero-copy parsing. Benchmarked to be highly performant.[^bench-res]
- Easy: Carefully designed and documented APIs. Handy percent-encoding utilities.
- Correct: Forbids unsafe code. Extensively fuzz-tested against other implementations.
[^bench-res]: In a benchmark
on an Intel Core i5-11300H processor, fluent-uri
parsed a 61-byte IRI
in ~85ns compared to ~125ns for iref
, iri-string
, and oxiri
.
Terminology
A URI reference is either a URI or a relative reference. If it starts with a scheme
(like http
, ftp
, mailto
, etc.) followed by a colon (:
), it is a URI. For example,
http://example.com/
and mailto:user@example.com
are URIs. Otherwise, it is
a relative reference. For example, //example.org/
, /index.html
, ../
, foo
,
?bar
, and #baz
are relative references.
An IRI (reference) is an internationalized version of URI (reference) which may contain non-ASCII characters.
Examples
-
Parse and extract components from a URI:
const SCHEME_FOO: &Scheme = new_or_panic; let s = "foo://user@example.com:8042/over/there?name=ferret#nose"; let uri = parse?; assert_eq!; let auth = uri.authority.unwrap; assert_eq!; assert_eq!; assert_eq!; assert!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!;
-
Build a URI using the builder pattern:
const SCHEME_FOO: &Scheme = new_or_panic; let uri = builder .scheme .authority_with .path .query .fragment .build .unwrap; assert_eq!;
-
Resolve a URI reference against a base URI:
let base = parse?; let uri_ref = parse?; assert_eq!; let uri_ref = parse?; assert_eq!; let uri_ref = parse?; assert_eq!;
-
Normalize a URI:
let uri = parse?; assert_eq!;
-
EStr
(Percent-encoded string slices):All components in a URI that may be percent-encoded are parsed as
EStr
s, which allows easy splitting and decoding:let s = "?name=%E5%BC%A0%E4%B8%89&speech=%C2%A1Ol%C3%A9%21"; let query = parse.unwrap.query.unwrap; let map: = query .split .map .map .collect; assert_eq!; assert_eq!;
-
EString
(A percent-encoded, growable string):You can encode key-value pairs to a query string and use it to build a URI reference:
let pairs = ; let mut buf = new; for in pairs assert_eq!; let uri_ref = builder .path .query .build .unwrap; assert_eq!;